package net.hzlj.platform.enterprise.task;

import net.hzlj.platform.enterprise.beans.UserBean;
import net.hzlj.platform.enterprise.config.HttpConfig;
import net.hzlj.platform.enterprise.helper.FileHelper;
import net.hzlj.platform.enterprise.utils.StringUtils;

import java.io.File;
import java.net.FileNameMap;
import java.net.URLConnection;

import cn.finalteam.okhttpfinal.HttpCycleContext;

/**
 * @ClassName: HttpRequestApi
 * @Description: 网络请求
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2016-8-18 09:52:29
 */
public class HttpRequestApi {


    /***
     * 登录
     * @param account   账号（身份证号）
     * @param passWord  密码
     * @param listener
     * @param httpCycleContext
     */
    public static void login(String account,String passWord,TaskListener listener,HttpCycleContext httpCycleContext){
        HttpRequestTask<UserBean> task = new HttpRequestTask(listener,httpCycleContext,UserBean.class);
        task.url = HttpConfig.URL_API_HOST + HttpConfig.POST_URL_LOGIN;
        task.type = HttpConfig.Type.LOGIN;
        task.params.addFormDataPart("account", account);
        task.params.addFormDataPart("passWord", passWord);
        task.execute();
    }

    /***
     * 修改密码
     * @param account   登录账号（身份证号码）
     * @param oldPwd    旧密码
     * @param newPwd    新密码
     * @param listener
     * @param httpCycleContext
     */
    public static void changePassWord(String account,String oldPwd,String newPwd,TaskListener listener,HttpCycleContext httpCycleContext){
        HttpRequestTask<UserBean> task = new HttpRequestTask(listener,httpCycleContext,UserBean.class);
        task.url = HttpConfig.URL_API_HOST + HttpConfig.POST_URL_CHANGE_PASSWORD;
        task.type = HttpConfig.Type.CHANGE_PASSWORD;
        task.params.addFormDataPart("account", account);
        task.params.addFormDataPart("oldPwd", oldPwd);
        task.params.addFormDataPart("newPwd", newPwd);
        task.execute();
    }

    /***
     * 获取当前登录者用户信息
     * @param listener
     */
    public static void getUserDetail(int userId,TaskListener listener){
        HttpRequestTask<UserBean> task = new HttpRequestTask(listener,UserBean.class);
        task.url = HttpConfig.URL_API_HOST + HttpConfig.POST_URL_SELECT_RYMC_BY_ID;
        task.type = HttpConfig.Type.SELECT_RYMC_BY_ID;
        task.params.addFormDataPart("id", userId);
        task.execute();
    }

    /***
     * 文件下载
     * @param url
     * @param listener
     */
    public static void downloadFile(String url,TaskListener listener){
        String fileName = StringUtils.getFileNameNoSuffixFromUrl(url);
        File saveFile = new File(FileHelper.getCachesDir(), fileName);
        HttpRequestDownloadTask<Integer> task = new HttpRequestDownloadTask<Integer>(listener,Integer.class);
        task.url = url;
        task.type = HttpConfig.Type.FILE_DOWNLOAD;
        task.file = saveFile;
        task.fileName = fileName;
        task.execute();
    }

    public static  String guessMimeType(String path) {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String contentTypeFor = fileNameMap.getContentTypeFor(path);
        if(contentTypeFor == null) {
            contentTypeFor = "application/octet-stream";
        }

        return contentTypeFor;
    }
}
